@extends('pc_admin.app')

@section('content')
<link rel="stylesheet" href="/css/pc-system/attendance-left.css">
<link rel="stylesheet" href="/css/pc-system/group-manage.css?v=2019052901">
<div class="mian-container">
    <div class="container-section group-manage">
        @include(('pc_sysadmin.attendance_left'))
        <div class="right-section">
            <div class="inner-div">
                <a class="create-group weui-btn_color" href="javascript:void(0);" data-id="1"><i class="icon iconfont icon-icon"></i>创建一级部门</a>
                @foreach($groups as $g)
                    <div class="first-floor weui-cells floor-delete">

                        <div class="weui-cell">
                            <div class="weui-cell__hd">
                                <a data-id="{{$g['id']}}" class="icon-delete" href="javascript:void(0);">删除</a>
                                <img class="floor-img" src="{{asset('/img/group-floora3.png')}}">
                            </div>
                            <div class="weui-cell__bd">
                                <p class="first-name">{{$g['group_name']}}</p>
                            </div>
                            <div class="weui-cell__ft">
                                <a href="{{url('/sysadmin/group/set-member?groupid='.$g['id'])}}"><i class="icon iconfont icon-weibiaoti--"></i>成员管理</a>
                                <a class="addson-group1" data-id="{{$g['id']}}" href="javascript:void(0);"><i class="icon iconfont icon-tianjiabumen"></i>添加子部门</a>
                            </div>
                        </div>
                        @foreach($g['child'] as $gc)
                            <div class="second-floor floor-delete">
                                <div class="weui-cell">
                                    <div class="weui-cell__hd">
                                        <a data-id="{{$gc['id']}}" class="icon-delete" href="javascript:void(0);">删除</a>
                                        <img class="floor-img" src="{{asset('/img/group-floorb3.png')}}">
                                    </div>
                                    <div class="weui-cell__bd">
                                        <p class="first-name">{{$gc['group_name']}}</p>
                                    </div>
                                    <div class="weui-cell__ft">
                                        <a href="{{url('/sysadmin/group/set-member?groupid='.$gc['id'])}}"><i class="icon iconfont icon-weibiaoti--"></i>成员管理</a>
                                        <a class="addson-group2" data-id="{{$gc['id']}}" href="javascript:void(0);"><i class="icon iconfont icon-tianjiabumen"></i>添加子部门</a>
                                    </div>
                                </div>
                                @foreach($gc['child'] as $gcc)
                                    <div class="third-floor floor-delete">
                                        <div class="weui-cell">
                                            <div class="weui-cell__hd">
                                                <a data-id="{{$gcc['id']}}" class="icon-delete" href="javascript:void(0);">删除</a>
                                                <img class="floor-img" src="{{asset('/img/group-floorc3.png')}}">
                                            </div>
                                            <div class="weui-cell__bd">
                                                <p class="first-name">{{$gcc['group_name']}}</p>
                                            </div>
                                            <div class="weui-cell__ft">
                                                <a href="{{url('/sysadmin/group/set-member?groupid='.$gcc['id'])}}"><i class="icon iconfont icon-weibiaoti--"></i>成员管理</a>
                                            </div>
                                        </div>
                                    </div>
                                @endforeach
                            </div>
                        @endforeach
                    </div>
                @endforeach
                {!! csrf_field() !!}
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function(){
        if($('.left-section').height()<$('.right-section').height()){
            $('.left-section').css('min-height',$('.right-section').height())
        }else{
            $('.right-section').css('min-height',$('.left-section').height());
        }
        //创建一级部门
        $('.create-group').click(function(){
            layer.open({
                id:1,
                type: 1,
                title:'创建部门',
                skin:'demo-class',
                area:['356px', '272px'],
                content: '<p class="layer-p">请为新部门输入名称</p>'
                +'<input type="text" value="" class="layer-input">',
                btn:['保存'],
                btn1: function (index,layero) {
                    var token = $('input[name=_token]').val();
                    var input = $('.layer-input').val().trim();
                    if(input.length == 0){
                        layer.msg('不能为空');
                        return false;
                    }
                    if(input.length > 9){
                        layer.msg('长度不能超过9');
                        return false;
                    }
                    $.ajax({
                        'url': '/sysadmin/group/add-group?_token='+token+'&fid=0',
                        'type': 'POST',
                        'data': '&name='+input,
                        error: function (request) {
                            layer.msg('网络错误');

                        },
                        success: function (data) {
                            if (data.status == 'success') {
                                layer.msg('操作成功');
                                setTimeout(function(){ window.location.href='/sysadmin/group/index';}, 1000);

                            } else {
                                layer.msg(data.msg);

                            }
                        },
                    });
                },
//                btn2:function (index,layero) {
//                    layer.close(index);
//                }
            });
        })
        //添加子部门
        $('body').on('click','.addson-group1',function(){
            var that = this;
            var fid = $(this).attr('data-id');
            layer.open({
                id:1,
                type: 1,
                title:'创建部门',
                skin:'demo-class',
                area:['356px', '272px'],
                content: '<p class="layer-p">请为新部门输入名称</p>'
                          +'<input type="text" value="" class="layer-input">',
                btn:['保存'],
                btn1: function (index,layero) {
                    var token = $('input[name=_token]').val();
                    var input = $('.layer-input').val();
                    $.ajax({
                        'url': '/sysadmin/group/add-group?_token='+token+'&fid='+fid,
                        'type': 'POST',
                        'data': '&name='+input,
                        error: function (request) {
                            layer.msg('error');
                        },
                        success: function (data) {
                            if (data.status == 'success') {
                                layer.msg('操作成功');
                                setTimeout(function(){ window.location.href='/sysadmin/group/index';}, 1000);
                            } else {
                                layer.msg('error');
                            }
                        },
                    });
                },

            });
        })
         $('body').on('click','.addson-group2',function(){
            var that = this;
             var fid = $(this).attr('data-id');
             layer.open({
                 id:1,
                 type: 1,
                 title:'创建部门',
                 skin:'demo-class',
                 area:['356px', '272px'],
                 content: '<p class="layer-p">请为新部门输入名称</p>'
                 +'<input type="text" value="" class="layer-input">',
                 btn:['保存'],
                 btn1: function (index,layero) {
                     var token = $('input[name=_token]').val();
                     var input = $('.layer-input').val();
                     $.ajax({
                         'url': '/sysadmin/group/add-group?_token='+token+'&fid='+fid,
                         'type': 'POST',
                         'data': '&name='+input,
                         error: function (request) {
                             layer.msg('error');
                         },
                         success: function (data) {
                             if (data.status == 'success') {
                                 layer.msg('操作成功');
                                 setTimeout(function(){ window.location.href='/sysadmin/group/index';}, 1000);
                             } else {
                                 layer.msg('error');
                             }
                         },
                     });
                 },

             });
        })
        //删除部门
        $('.delete-group').click(function(){
            $('.icon-shanchu3').toggle();
        })
        $('.icon-delete').click(function(){
            var that=this;
            var gid = $(this).attr('data-id');
            layer.confirm('确认删除部门，该部门下成员会放到默认部门“我的同事”中',{
                btn: ['确定', '取消']
            }, function(index){
                var token = $('input[name=_token]').val();
                $.ajax({
                    'url': '/sysadmin/group/del-group?_token='+token+'&groupid='+gid,
                    'type': 'POST',
                    'data': '',
                    error: function (request) {
                        console.log("error");
                    },
                    success: function (data) {
                        if (data.status == 'success') {
                            layer.msg("操作成功");
                            setTimeout(function(){ window.location.href='/sysadmin/group/index';}, 1000);
                        } else {
                            layer.msg(data.msg);
                        }
                    },
                });
                layer.close(index);
            });
        })
    })
    $('.title-list.top-list').addClass('active');
</script>
@endsection